package com.penghaisoft.microwms.base.web.expose.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.penghaisoft.microwms.base.web.auth.service.IBaseFactoryService;
import com.penghaisoft.microwms.base.web.auth.service.IUserService;
import com.penghaisoft.microwms.base.web.data.service.IMdContractService;
import com.penghaisoft.microwms.base.web.data.service.IMdLocationService;
import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.base.model.BaseFactory;
import com.penghaisoft.microwms.common.base.model.BaseUser;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.MdContract;
import com.penghaisoft.microwms.common.data.model.MdLocation;
import com.penghaisoft.microwms.common.dto.HandResp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.dto.UserInfo;
import com.penghaisoft.microwms.common.page.Pager;

/**
 * @program: micro-wms
 * @description: 免登录手持接口
 * @author: zhangx
 * @create: 2018-05-17 15:23
 **/
@RestController
@RequestMapping(value = "/expose/hand")
public class ExposeHandController extends BaseController {

	@Autowired
	private IUserService userService;

	@Autowired
	private IBaseFactoryService baseFactoryService;

	@Autowired
	private IMdLocationService locationService;

	@Autowired
	private IMdContractService mdContractService;

	/**
	 * 获取系统时间
	 * 
	 * @return
	 */
	@PostMapping("systime")
	public HandResp systime() {
		HandResp result = new HandResp();
		result.setCode("1");
		result.setValue(Constant.sdfYMDHMS.format(new Date()));
		return result;
	}

	/**
	 * 连接测试
	 * 
	 * @return
	 */
	@PostMapping("connectiontest")
	public HandResp connectiontest() {
		HandResp result = new HandResp();
		result.setCode("1");
		return result;
	}

	/**
	 * 手持用户信息
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("userinfo")
	public HandResp getUserInfo(@RequestBody BaseUser param) {
		HandResp result = new HandResp();
		BaseUser user = userService.queryOne(param);
		if (user != null) {
			String userId = user.getUserId();
			List<BaseFactory> factories = baseFactoryService.queryUserFactory(userId);
			List<UserInfo> infos = new ArrayList<>();
			if (factories != null && factories.size() == 1) {
				result.setCode(Constant.SUCCESS);
				BaseFactory factory = factories.get(0);
				UserInfo info = new UserInfo();
				info.setLoginName(user.getLoginName());
				info.setUserName(user.getUserName());
				info.setFactoryCode(factory.getFactoryCode());
				info.setFactoryName(factory.getFactoryName());
				infos.add(info);
//				5 该工厂是否允许手动输入
//                boolean allowHandInputFlag = allowHandInput(factory.getFactoryCode());
//                if (allowHandInputFlag) {
//                    result.setValue("1");
//                }else {
//                    result.setValue("0");
//                }
				result.setList(infos);
			} else {
				result.setCode(Constant.FAIL);
				result.setMsg("此用户多工厂！");
			}
		} else {
			result.setCode(Constant.FAIL);
			result.setMsg(param.getLoginName() + " 此账号不存在!");
		}
		return result;
	}

	/**
	 * 获取用户登录工作区下的库位
	 * 
	 * @param param
	 * @return
	 */
	@PostMapping("locations")
	public HandResp getUserLocations(@RequestBody UserInfo param) {
		HandResp result = new HandResp();

//		1查询出用户的工作区
		String workAreaCode = param.getWorkareaCode();
//		2查询出每个工作区下的库位
		List<MdLocation> locations = new ArrayList<>();
		if (workAreaCode != null) {
			MdLocation mdLocation = new MdLocation();
			mdLocation.setWorkArea(workAreaCode);
			locations.add(mdLocation);
			locations = locationService.queryAllUnderWorkArea(locations);
			List<String> locs = new ArrayList<>();
			for (MdLocation location : locations) {
				locs.add(location.getLocationCode());
			}
			result.setValues(locs);
			result.setCode(Constant.SUCCESS);
		} else {
			result.setCode(Constant.FAIL);
			result.setMsg("请输入工作区编码！");
		}

		return result;
	}

	/**
	 * @Title queryContractList
	 * @Description 手持合同号下拉框
	 * @author luoteng
	 * @date 2018年6月6日:上午9:39:53
	 * @param mdContract
	 * @return HandResp
	 */
	@PostMapping("contract/select/get")
	public HandResp queryContractList(@RequestBody MdContract mdContract) {
		HandResp rtnResp = new HandResp();
		mdContract.setActiveFlag("1");
		List<MdContract> list = mdContractService.queryList(mdContract);
		rtnResp.setCode(Constant.SUCCESS);
		rtnResp.setList(list);
		return rtnResp;
	}

	/**
	 * @Title: queryCustomerByContractNo @Description: 根据合同号获取客户名称 @param @param
	 * mdContract @param @return 参数说明 @return HandResp 返回类型 @author luoteng @date
	 * 2018年10月25日 下午5:30:13 @throws
	 */
	@PostMapping("contract/customer/get")
	public HandResp queryCustomerByContractNo(@RequestBody MdContract mdContract) {
		HandResp rtnResp = new HandResp();
		mdContract.setActiveFlag("1");
		List<MdContract> list = mdContractService.queryList(mdContract);
		if (list != null) {
			rtnResp.setValue(list.get(0).getUserDefined1());
			rtnResp.setCode(Constant.SUCCESS);
		} else {
			rtnResp.setCode(Constant.FAIL);
		}
		return rtnResp;
	}

	/**
	 * @Title: list
	 * @Description: 获取合同号列表
	 * @date 2019年3月8日 上午11:36:29
	 * @author luoteng
	 * @param page
	 * @param rows
	 * @param mdContract
	 * @return TransResult
	 */
	@PostMapping("contract/page/get")
	public TransResult list(@RequestBody MdContract mdContract) {

		Pager<MdContract> result = mdContractService.findListByCondition(mdContract.getPage(), mdContract.getRows(), mdContract);
		return success(result);
	}

}
